import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/data/api/special_list.dart';
import 'package:enterprise_v6_app/app/data/common/charts_pie.dart';
import 'package:enterprise_v6_app/app/data/entities/application/special_list/work_count_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/special_list/work_history_entity.dart';

class SpecialPersonalDataController extends GetxController {
  // 测试数据 作业异常
  List test2 = [
    {'name': '动火作业', 'value': 0.3, 'num': 80},
    {'name': '受限空间', 'value': 0.2, 'num': 60},
    {'name': '盲板抽堵', 'value': 0.1, 'num': 40},
    {'name': '高处作业', 'value': 0.4, 'num': 20},
    {'name': '吊装作业', 'value': 0.5, 'num': 40},
    {'name': '临时用电', 'value': 0.6, 'num': 60},
    {'name': '动土作业', 'value': 0.7, 'num': 80},
    {'name': '断路作业', 'value': 1, 'num': 100},
  ];

  ///当前作业(任务板) 作业总数
  int taskTotal = 0;

  /// 当前作业(任务板) 作业数据
  WorkCountEntity workCountEntity = WorkCountEntity();

  ///作业异常数据
  List<WorkHistoryEntity> workHistoryList = [];

  ///历史概述总数
  String historyTotal = '';

  ///作业历史概述
  List<ChartData> chartList = [
    // ChartData('动火作业', 15, const Color(0xffFD6B6B), text: '20%'),
    // ChartData('受限空间', 20, const Color(0xffF7BA1E), text: '30%'),
    // ChartData('盲板抽堵', 30, const Color(0xffFC9900), text: '40%'),
    // ChartData('高处作业', 32, const Color(0xff14C9C9), text: '50%'),
    // ChartData('吊装作业', 42, const Color(0xff2C6CFF), text: '60%'),
    // ChartData('临时用电', 10, const Color(0xffB17AFF), text: '70%'),
    // ChartData('动土作业', 6, const Color(0xffFD6B6B), text: '80%'),
    // ChartData('断路作业', 62, const Color(0xff3491FA), text: '90%'),
  ];

  /// 作业历史概述颜色
  List<Color> chartColorList = [
    const Color(0xffFD6B6B),
    const Color(0xffF7BA1E),
    const Color(0xffFC9900),
    const Color(0xff14C9C9),
    const Color(0xff2C6CFF),
    const Color(0xffB17AFF),
    const Color(0xffFD6B6B),
    const Color(0xff3491FA),
  ];

  @override
  void onInit() {
    super.onInit();
    // 获取作业类型异常数量统计
    getTicketReceiptValidDate();
    // 获取特殊作业历史概览
    getSpecialWorkHistory();
    // 获取任务板(作业状态数量统计)
    getTaskBoard();
  }

  /// 获取作业类型异常数量统计
  getTicketReceiptValidDate() async {
    final res = await SpecialListApis.getWorkTypeException(1).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        workHistoryList = result.data!;
        int total = 0;
        // 获取作业总数
        for (var item in workHistoryList) {
          total += item.ticketCount!;
        }
        // 如果总数不为空
        if (total != 0) {
          // 填充页面作业异常数据
          for (var item in workHistoryList) {
            item.value = item.ticketCount! / total;
          }
        }
        update();
      }
    });
  }

  /// 获取特殊作业历史概览
  getSpecialWorkHistory() async {
    final res = await SpecialListApis.getSpecialWorkHistory(1).execute();
    res.onSuccess((result) {
      if (result.data != null && result.data!.isNotEmpty) {
        final Map<String, dynamic> map = result.data!;
        final List<WorkHistoryEntity> workHistoryList = [];
        //历史概述总数
        historyTotal = map.keys.first;
        //历史概述值
        final List workList = map.values.first as List;
        //添加数据
        for (var item in workList) {
          workHistoryList
              .add(WorkHistoryEntity.fromJson(item as Map<String, dynamic>));
        }
        //填充页面数据
        for (int i = 0; i < workHistoryList.length; i++) {
          final item = workHistoryList[i];
          chartList.add(ChartData(
            item.ticketTypeName!,
            item.ticketCount!.toDouble(),
            chartColorList[i],
            text: '${i + 2}0%',
          ));
        }
      }
    });
  }

  /// 获取任务板(作业状态数量统计)
  getTaskBoard() async {
    final res = await SpecialListApis.getTaskBoard(1).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        final WorkCountEntity data = result.data!;
        workCountEntity = data;
        taskTotal =
            data.acceptedCount! + data.issuedCount! + data.notIssuedCount!;
      }
      update();
    });
  }
}
